package com.kordar.admin.kernel.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kordar.admin.kernel.util.HelperUtil;
import lombok.Data;
import lombok.ToString;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Data
@ToString
public class QueryWrapperEntity<T> {
    private int page = 1;
    private int pagesize = 10000;
    private Map<String, String> conditions;
    private Map<String, String> sort;
    private Map<String, String> data;
    private QueryWrapper<T> query = new QueryWrapper<>();

    public QueryWrapperEntity() {
    }

    public Page<T> queryPage() {
        return new Page<T>(this.page, this.pagesize);
    }

    public Page<T> queryPage(Integer pagesize) {
        return new Page<T>(1, pagesize);
    }

    public QueryWrapperEntity<T> where(Class<T> clzz) {

        Map<String, Class<?>> parallel = new HashMap<>();
        Field[] fields = clzz.getDeclaredFields();
        Field field = null;
        for (int i = 0; i < fields.length; i++) {
            try {
                field = fields[i];
                field.setAccessible(true); // 开启访问权限
                parallel.put(field.getName(), field.getType());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        String lst = conditions.remove("yak_lst");
        String lstExt = conditions.remove("yak_lst_ext");
        for (Map.Entry<String, String> m : conditions.entrySet()) {

            String key = m.getKey();
            if ("yak_lst_txt".equals(key)) {
                key = lst;
            }

            if (! (parallel.containsKey(key) || parallel.containsKey(HelperUtil.lineToHump(key)))) {
                continue;
            }

            String value = m.getValue();
            Class<?> type = parallel.get(key);

            if ("yak_lst_txt".equals(m.getKey()) && !value.isEmpty()) {
                if ("EQ".equals(lstExt)) {
                    compareEq(type, key, value);
                }
                if ("LIKE".equals(lstExt)) {
                    compareLike(type, key, value);
                }
            } else if (!value.isEmpty()) {
                compareEq(type, key, value);
            }
        }
        return this;
    }

    protected void compareEq(Class<?> type, String key, String value) {
        if (type == Integer.class) {
            this.query = this.query.eq(key, Integer.parseInt(value));
        } else if (type == Long.class) {
            this.query = this.query.eq(key, Long.valueOf(value));
        } else {
            this.query = this.query.eq(key, value);
        }
    }

    protected void compareLike(Class<?> type, String key, String value) {
        if (type == Integer.class) {
            this.query = this.query.like(key, Integer.parseInt(value));
        } else if (type == Long.class) {
            this.query = this.query.like(key, Long.valueOf(value));
        } else {
            this.query = this.query.like(key, value);
        }
    }

    public QueryWrapperEntity<T> orderBy() {
        for (Map.Entry<String, String> m : sort.entrySet()) {
            if ("asc".equals(m.getValue())) {
                this.query = this.query.orderByAsc(m.getKey());
            }
            if ("desc".equals(m.getValue())) {
                this.query = this.query.orderByDesc(m.getKey());
            }
        }
        return this;
    }
}
